# backend/models.py
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from backend.database import Base


class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    full_name = Column(String)
    is_admin = Column(Boolean, default=False)
    created_at = Column(DateTime)

    added_books = relationship("Book", back_populates="added_by_user")
    borrow_records_borrower = relationship("BorrowRecord", foreign_keys="[BorrowRecord.borrower_id]",
                                           back_populates="borrower")
    borrow_records_lender = relationship("BorrowRecord", foreign_keys="[BorrowRecord.lender_id]",
                                         back_populates="lender")
    library_memberships = relationship("LibraryMember", back_populates="user")


class Library(Base):
    __tablename__ = "libraries"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    invite_code = Column(String, unique=True)
    owner_id = Column(Integer, ForeignKey("users.id"))
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime)

    books = relationship("Book", back_populates="library")
    members = relationship("LibraryMember", back_populates="library")


class LibraryMember(Base):
    __tablename__ = "library_members"
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    library_id = Column(Integer, ForeignKey("libraries.id"))
    join_date = Column(DateTime)
    is_owner = Column(Boolean, default=False)

    user = relationship("User", back_populates="library_memberships")
    library = relationship("Library", back_populates="members")


class Book(Base):
    __tablename__ = "books"
    id = Column(Integer, primary_key=True, index=True)
    isbn = Column(String, index=True)
    title = Column(String)
    author = Column(String)
    cover_url = Column(String)
    library_id = Column(Integer, ForeignKey("libraries.id"))
    status = Column(Integer, default=1)  # 0-下架,1-上架,2-借出
    added_by = Column(Integer, ForeignKey("users.id"))
    added_at = Column(DateTime)

    library = relationship("Library", back_populates="books")
    added_by_user = relationship("User", back_populates="added_books")
    borrow_records = relationship("BorrowRecord", back_populates="book")


class BorrowRecord(Base):
    __tablename__ = "borrow_records"
    id = Column(Integer, primary_key=True, index=True)
    book_id = Column(Integer, ForeignKey("books.id"))
    borrower_id = Column(Integer, ForeignKey("users.id"))
    lender_id = Column(Integer, ForeignKey("users.id"))
    borrow_date = Column(DateTime)
    return_date = Column(DateTime)
    status = Column(Integer, default=0)  # 0-预约中,1-借出中,2-已归还

    book = relationship("Book", back_populates="borrow_records")
    borrower = relationship("User", foreign_keys=[borrower_id], back_populates="borrow_records_borrower")
    lender = relationship("User", foreign_keys=[lender_id], back_populates="borrow_records_lender")
